﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Frame.Lib.Common;
using Frame.Lib.Core.Base;
using Frame.Lib.Utilities;

namespace Jzy.DAL.DocManger
{
    public class DirDAL : BaseDAL
    {
        public Dictionary<int, int> GetCreatorIDs(List<int> ids)
        {
            string strIDs = ConvertUtil.ConvertToDbStr(ids);
            string strSql = "select ID,CreatorID from " + OTableName.DocDir + " where ID in (" + strIDs + ")";
            DataTable dt = dbOper.Query(strSql);
            Dictionary<int, int> ditCreatorIDs = new Dictionary<int, int>();
            foreach (DataRow dr in dt.Rows)
            {
                ditCreatorIDs.Add((int)dr["ID"], (int)dr["CreatorID"]);
            }
            return ditCreatorIDs;
        }

        public bool Exist(int upperID, string strName, string id, bool isPrivate)
        {
            string sql = "select count(*) from " + OTableName.DocDir + " where UpperID = @p0 and [Name] = @p1";

            if (isPrivate)
            {
                sql += " and IsPrivate=1 and CreatorID = " + AppSession.UserID;
            }
            else
            {
                sql += " and IsPrivate = 0";
            }

            if (!string.IsNullOrEmpty(id))
            {
                sql += " and ID != @p2";
            }
            int count = dbOper.GetScalar<int>(sql, upperID, strName, id);
            bool flag = count > 0 ? true : false;
            return flag;
        }

        public bool ExistCode(string code, string id, bool isPrivate)
        {
            string sql = "select count(*) from " + OTableName.DocDir + " where [Code] = @p0";
            if (isPrivate)
            {
                sql += " and IsPrivate=1 and CreatorID = " + AppSession.UserID;
            }
            else
            {
                sql += " and IsPrivate = 0";
            }
            if (!string.IsNullOrEmpty(id))
            {
                sql += " and ID != @p1";
            }
            int count = dbOper.GetScalar<int>(sql, code, id);
            bool flag = count > 0 ? true : false;
            return flag;
        }
    }

}
